package com.cw.jeeyt.controller.system;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.system.SystemIconBo;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.jeeyt.service.intf.system.SystemIconService;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 图标配置表控制层
 *
  * @author chenw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/systemIcon")
public class SystemIconController extends BaseController {
    @Resource
    private SystemIconService systemIconService;

    /**
     * 跳转到图标配置表列表查询页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() throws Exception {
        ModelAndView mv = new ModelAndView("system/systemIcon_index");
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/listSystemIcon")
    public ResultData listSystemIcon(@RequestBody DataTable dataTable) throws Exception {

        SystemIconBo systemIconBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            SystemIconBo.class);
        if (systemIconBo == null) {
            systemIconBo = new SystemIconBo();
        }
        PageInfo<SystemIconBo> pageInfo = systemIconService.queryByPage(systemIconBo, dataTable.getStart(),
            dataTable.getLength());
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到图标配置表表单页面
     *
     * @param id 主键
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView editForm(String id, String operation) throws Exception {

        ModelAndView mv = new ModelAndView("system/systemIcon_form");
        SystemIconBo systemIconBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            systemIconBo = new SystemIconBo();
        } else {
            systemIconBo = systemIconService.getSystemIconById(id);
        }
        mv.addObject("systemIcon", systemIconBo);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }

    /**
     * 跳转到图标配置表表单页面
     *
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-batForm")
    public ModelAndView batEdit(String operation) {

        ModelAndView mv = new ModelAndView("system/systemIcon_batForm");
        mv.addObject("operation", operation);
        return mv;
    }

    /**
     * 根据操作类型保存图标配置表数据
     *
     * @param systemIconBo
     * @param operation add 新增  edit 修改
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/saveSystemIcon", method = RequestMethod.POST)
    public JsonResultDto saveSystemIcon(SystemIconBo systemIconBo, String operation) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                systemIconService.insert(systemIconBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                systemIconService.update(systemIconBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            jsonResultDto.setMsg("保存失败:" + e.getMessage());
            e.printStackTrace();
            logger.error("保存图标配置表失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 批量保存图标配置表数据
     *
     * @param systemIconBo
     * @param operation add 新增  edit 修改
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/batSaveSystemIcon", method = RequestMethod.POST)
    public JsonResultDto batSaveSystemIcon(SystemIconBo systemIconBo, String operation) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                systemIconService.batInsert(systemIconBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                systemIconService.update(systemIconBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            jsonResultDto.setMsg("保存失败:" + e.getMessage());

            e.printStackTrace();
            logger.error("保存图标配置表失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 删除图标配置表
     *
     * @param systemIconBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto delete(SystemIconBo systemIconBo) throws Exception {
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemIconService.delete(systemIconBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();

            jsonResultDto.setMsg("删除失败：" + e.getMessage());
            logger.error("删除图标配置表失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 加载系统图标
     *
     * @param systemIconBo
     * @throws Exception
     */
    @RequestMapping(value = "/loadIcon")
    @ResponseBody
    public JsonResultDto loadIcon(SystemIconBo systemIconBo) {
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            List<SystemIconBo> iconList = systemIconService.loadIcon(systemIconBo);
            jsonResultDto.setSuccess(true);
            jsonResultDto.setData(iconList);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("删除失败：" + e.getMessage());
            logger.error("删除图标配置表失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
}
